package cl.tinet.finalexam.dao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import cl.tinet.finalexam.model.ClassroomModel;
import cl.tinet.finalexam.model.ReserveModel;
import cl.tinet.finalexam.model.UserModel;

public class SystemBDDAO {

    /**
     * Persistencia a Base de datos.
     */
    @PersistenceContext(unitName = "MysqlDS")
    protected EntityManager entityManager;

    /**
     * Constructor que recibe Persistencia a Base de datos.
     */
    public SystemBDDAO(EntityManager em) {
        this.entityManager = em;
    }

    /**
     * 
     * Busqueda por username.
     * 
     * @param usernameToSearch
     *            Usuario a buscar
     * @return usuario encontrado
     */
    public UserModel searchByUsername(String usernameToSearch) {

        UserModel foundUser = null;

        String sql = "Select u from UserModel u Where u.username=:userToSearch";
        Query sqlQuery =
                entityManager.createQuery(sql).setParameter("userToSearch",
                        usernameToSearch);
        foundUser = (UserModel) sqlQuery.getSingleResult();

        return foundUser;

    }

    /**
     * 
     * Busqueda usuario por ID.
     * 
     * @param idToSearch
     *            Id del usuario
     * @return usuario encontrado
     */

    public UserModel searchByUserID(int idToSearch) {

        UserModel foundUser = null;

        String sql = "Select u from UserModel u Where userID=:userToSearch";
        Query sqlQuery =
                entityManager.createQuery(sql).setParameter("userToSearch",
                        idToSearch);
        foundUser = (UserModel) sqlQuery.getSingleResult();

        return foundUser;

    }
    

    /**
     * 
     * Busqueda reserva por ID reserva.
     *
     * @param reserveIDToSearch
     * @return Reserva encontrada
     */
    public ReserveModel searchByReserveID(int reserveIDToSearch) {

        ReserveModel foundReserve = null;

        String sql =
                "Select u from ReserveModel u Where u.idReserve=:idToSearch";
        Query sqlQuery =
                entityManager.createQuery(sql).setParameter("idToSearch",
                        reserveIDToSearch);
        foundReserve = (ReserveModel) sqlQuery.getSingleResult();

        return foundReserve;

    }

    /**
     * 
     * Busqueda pieza por ID
     *
     * @param RoomIDToSearch
     * @return Encontrada
     */
    public ClassroomModel searchByRoomID(int RoomIDToSearch) {

        ClassroomModel foundRoom = null;

        String sql = "Select u from ClassroomModel u Where salaID=:idToSearch";
        Query sqlQuery =
                entityManager.createQuery(sql).setParameter("idToSearch",
                        RoomIDToSearch);
        foundRoom = (ClassroomModel) sqlQuery.getSingleResult();

        return foundRoom;
    }

    /**
     * 
     * Añadir Reserva
     *
     * @param objectToPersist
     */
    public void addReserve(ReserveModel objectToPersist) {
        entityManager.merge(objectToPersist);
    }

    /**
     * 
     * Añadir pieza.
     *
     * @param objectToPersist
     */
    public void addRoom(ClassroomModel objectToPersist) {
        entityManager.persist(objectToPersist);
    }

    /**
     * 
     * Añadir usuario.
     *
     * @param objectToPersist
     */
    public void addUser(UserModel objectToPersist) {
        entityManager.merge(objectToPersist);
    }

    /**
     * 
     * Remover Reserva.
     *
     * @param reserveToRemove
     */
    public void removeReserve(ReserveModel reserveToRemove) {
        entityManager.remove(reserveToRemove);
    }

    /**
     * 
     * Remover Pieza.
     *
     * @param roomToRemove
     */
    public void removeRoom(ClassroomModel roomToRemove) {
        entityManager.remove(roomToRemove);
    }

    /**
     * 
     * Busqueda todos usuarios.
     *
     * @return
     */
    public Collection<UserModel> allUsers() {
        Collection<UserModel> foundUser = null;

        String sql = "Select u from UserModel u";
        Query sqlQuery = entityManager.createQuery(sql);

        foundUser = sqlQuery.getResultList();

        return foundUser;
    }
}
